<template>
  <div class="userAdd">
    <div class="title">
      <span>用户管理 | 新增用户</span>
    </div>
    <div class="content">
      <h3>添加用户</h3>
      <form @submit.prevent="addUser">
        <div class="items">
          <span>用户名称&nbsp;:</span>
          <input
            type="text"
            v-model="username"
            @input="validateUsername"
            required
          />
          <div v-if="UnerrorMessage" ref="uNameError" class="error">
            {{ UnerrorMessage }}
          </div>
        </div>
        <div class="items">
          <span>用户密码&nbsp;:</span>
          <input
            type="password"
            v-model="password"
            @input="validatePassword"
            required
          />
          <div v-if="PwerrorMessage" ref="pwdError" class="error">
            {{ PwerrorMessage }}
          </div>
        </div>
        <div class="items">
          <span>用户角色&nbsp;:</span>
          <input type="text" value="普通用户" disabled />
        </div>
        <button type="submit" class="addUser">添加</button>
      </form>
    </div>
  </div>
</template>
  
  <script>
import { userRegister } from "@/api/all";
import { Message } from "element-ui";
export default {
  name: "userAdd",
  data() {
    return {
      username: "",
      password: "",
      usernamePass: false,
      passwordPass: false,
      UnerrorMessage: "用户名必须是 1 - 7 个汉字，不得输入特殊字符",
      PwerrorMessage:
        "密码长度应为 8 - 20 位，且必须包含至少一个大写字母、一个小写字母和一个数字",
    };
  },
  methods: {
    validatePassword() {
      const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,20}$/;
      if (!regex.test(this.password) && this.password !== "") {
        this.$refs.pwdError.style.opacity = 1;
        this.passwordPass = false;
      } else {
        this.passwordPass = true;
        this.$refs.pwdError.style.opacity = 0;
      }
    },
    validateUsername() {
      const regex = /^[\u4e00-\u9fa5]{1,8}$/;
      if (!regex.test(this.username) && this.username !== "") {
        this.$refs.uNameError.style.opacity = 1;
        this.usernamePass = false;
      } else {
        this.$refs.uNameError.style.opacity = 0;
        this.usernamePass = true;
      }
    },
    async addUser() {
      if (!this.passwordPass || !this.usernamePass)
        Message.error("用户名或密码格式错误！！！");
      else {
      let params = {
        id: 1,
        username: this.username,
        password: this.password,
        role: "user",
        image: "null",
      };
      await userRegister(params)
        .then((res) => {
          if (res.data.code === 200) {
            if (res.data.message === "Ok") {
              Message.success("用户添加成功");
              this.$router.push({ name: "userList" });
            } else {
              Message.error("该用户已经添加过了！");
            }
          }
        })
        .catch((error) => {
          console.log(error);
        });
      }
    },
  },
};
</script>
  
  <style scoped>
.userAdd {
  min-height: 100vh;
}
.title {
  display: flex;
  height: 50px;
  line-height: 50px;
  color: #666666;
  margin-bottom: 20px;
  font-size: 18px;
}
.content {
  display: flex;
  flex-direction: column;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  width: 500px;
  height: 500px;
  margin: 0px auto;
  background: rgba(232, 231, 231, 0.6);
  box-shadow: 10px 10px 25px #666666;
  border-radius: 10px;
}
h3 {
  display: block;
  text-align: center;
  color: #666666;
  height: 50px;
  margin-bottom: 40px;
}
.items {
  width: auto;
  height: 90px;
}
.items span {
  display: inline-block;
  width: 80px;
  text-align: left;
  color: #666666;
  font-size: 16px;
}
.items input {
  height: 40px;
  padding: 5px;
  border: 1px solid #c0c0c0;
  border-radius: 10px;
  width: 300px;
  font-size: 16px;
  color: #666666;
  vertical-align: middle;
}
.addUser {
  background: rgba(42, 81, 77, 1);
  color: #fff;
  width: 400px;
  height: 35px;
  border-radius: 8px;
  margin: 10px auto 30px;
  border: 1px solid #215c3d;
  transition: all 0.3s;
}
.addUser:hover {
  cursor: pointer;
  box-shadow: 15px 5px 25px #929493;
}
.error {
  color: rgb(176, 19, 19);
  font-size: 12px;
  width: 400px;
  padding: 8px 30px 0px 10px;
  opacity: 0;
  transition: all 0.5s;
}
</style>